package org.locker.device.controller.admin;

import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.locker.common.idempotent.annotation.RepeatSubmit;
import org.locker.common.log.annotation.Log;
import org.locker.common.web.core.BaseController;
import org.locker.common.mybatis.core.page.PageQuery;
import org.locker.common.core.domain.R;
import org.locker.common.core.validate.AddGroup;
import org.locker.common.core.validate.EditGroup;
import org.locker.common.log.enums.BusinessType;
import org.locker.common.excel.utils.ExcelUtil;
import org.locker.domain.vo.FeedbackVo;
import org.locker.domain.bo.FeedbackBo;
import org.locker.service.IFeedbackService;

/**
 * 反馈(投诉建议)
 *
 * @author winnie
 * @date 2023-10-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/device/feedback")
public class FeedbackController extends BaseController {

    private final IFeedbackService feedbackService;

    /**
     * 查询反馈(投诉建议)列表
     */
    @SaCheckPermission("device:feedback:list")
    @GetMapping("/list")
    public R<Page<FeedbackVo>> list(FeedbackBo bo, PageQuery pageQuery) {
        return R.ok(feedbackService.queryPageList(bo, pageQuery));
    }

    /**
     * 导出反馈(投诉建议)列表
     */
    @SaCheckPermission("device:feedback:export")
    @Log(title = "反馈(投诉建议)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(FeedbackBo bo, HttpServletResponse response) {
        List<FeedbackVo> list = feedbackService.queryList(bo);
        ExcelUtil.exportExcel(list, "反馈(投诉建议)", FeedbackVo.class, response);
    }

    /**
     * 获取反馈(投诉建议)详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("device:feedback:query")
    @GetMapping("/{id}")
    public R<FeedbackVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(feedbackService.queryById(id));
    }

    /**
     * 新增反馈(投诉建议)
     */
    @SaCheckPermission("device:feedback:add")
    @Log(title = "反馈(投诉建议)", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody FeedbackBo bo) {
        return toAjax(feedbackService.insertByBo(bo));
    }

    /**
     * 修改反馈(投诉建议)
     */
    @SaCheckPermission("device:feedback:edit")
    @Log(title = "反馈(投诉建议)", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody FeedbackBo bo) {
        return toAjax(feedbackService.updateByBo(bo));
    }

    /**
     * 删除反馈(投诉建议)
     *
     * @param ids 主键串
     */
    @SaCheckPermission("device:feedback:remove")
    @Log(title = "反馈(投诉建议)", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(feedbackService.deleteWithValidByIds(List.of(ids), true));
    }
}
